<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>

  <body>

<%
    String boxName = request.getParameter("boxName");
    if (boxName == null) {
        boxName = "commonBox";
    }

%>



<%
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key boxKey = KeyFactory.createKey("commonBox", boxName);

    Query query = new Query("Message");
    List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if (greetings.isEmpty()) {
        %>
        <p>inBox '<%= boxName %>' has no messages.</p>
        <%
    } else {
        %>
        <p>Messages inBox '<%= boxName %>'.</p>
        <%
        for (Entity greeting : greetings) {%>
        <div class = "message">
	<div class = "avatar">
	www
	</div>
	<div class = "rightMsg">
		<div class = "msgHeading">
		
        <%if (greeting.getProperty("userName") == null) {
                %>
                <strong>Anonimous user wrote:</strong>
                <%
            } else {
                %>
                
                <strong>User <%= (greeting.getProperty("userName")) %> wrote:</strong>
                <%
            }
            %>
            
            		</div>
		<div class = "msgContent">
		<%= greeting.getProperty("text") %>
        </div>
		<div class = "msgBottom">
		Likes: <%= (greeting.getProperty("likes")) %> 
		</div>
	</div>
	<div class = "msgButtons">
		<form action="/web" method="post">
      		<div><input class="msgButton" type="submit" value="Like" /></div>
      		<input type="hidden" name="action" value="likeMsg"/>
      		<input type="hidden" name="id" value="<%= greeting.getKey().getId() %>"/>
    	</form>
    	<form action="/web" method="post">
      		<div><input class="msgButton" type="submit" value="Delete" /></div>
      		<input type="hidden" name="action" value="delMsg"/>
      		<input type="hidden" name="id" value="<%= greeting.getKey().getId() %>"/>
    	</form>
	</div>
</div>
            <%
        }
    }
%>

<div class = "message">
	<div class = "avatar">
	www
	</div>
	<div class = "rightMsg">
		<div class = "msgHeading">
		<strong>User something wrote:</strong>
		</div>
		<div class = "msgContent">
		text here
		</div>
		<div class = "msgBottom">
		This is bottom
		</div>
	</div>
	<div class = "msgButtons">
		<form action="/web" method="post">
      		<div><input class="msgButton" type="submit" value="Like" /></div>
      		<input type="hidden" name="action" value="like"/>
    	</form>
    	<form action="/web" method="post">
      		<div><input class="msgButton" type="submit" value="Delete" /></div>
      		<input type="hidden" name="action" value="like"/>
    	</form>
	</div>
</div>

    <form class="postForm" action="/web" method="post">
      <div><textarea name="userName" rows="1" cols="30" placeholder="User"></textarea></div>
      <div><textarea name="text" rows="3" cols="60" placeholder="Message..."></textarea></div>
      <div><input class="postButton" type="submit" value="Post and Share" /></div>
      <input type="hidden" name="boxName" value="commonBox"/>
      <input type="hidden" name="nickName" value="commonBox"/>
      <input type="hidden" name="geo" value="commonBox"/>
      <input type="hidden" name="language" value="commonBox"/>
      <input type="hidden" name="country" value="commonBox"/>
      <input type="hidden" name="likes" value="put"/>
      <input type="hidden" name="date" value="put"/>
      <input type="hidden" name="action" value="postMsg"/>
      <input type="hidden" name="userName" value="Nanajkin"/>
    </form>
    <form class="postForm" action="/web" method="post">
      <div><textarea name="id" rows="1" cols="30" placeholder="User"></textarea></div>
      <div><input class="postButton" type="submit" value="Post and Share" /></div>
      <input type="hidden" name="boxName" value="commonBox"/>
      <input type="hidden" name="nickName" value="commonBox"/>
      <input type="hidden" name="geo" value="commonBox"/>
      <input type="hidden" name="language" value="commonBox"/>
      <input type="hidden" name="country" value="commonBox"/>
      <input type="hidden" name="likes" value="put"/>
      <input type="hidden" name="date" value="put"/>
      <input type="hidden" name="action" value="postUser"/>
      <input type="hidden" name="userName" value="Alo"/>
      <input type="hidden" name="text" value="Garage"/>
    </form>

  </body>
</html>